/**
 * Created by isoft-work 5 on 11/16/14.
 */

$(document).ready(function(){
//   render menu
   _lstCart = null;
   renderLoadInfoUser();
   renderMenu();
   renderCart();
//   event
   bindEventLoadInfoUser();
});

function renderLoadInfoUser() {
   var userName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName'): ISS.COOKIE.getCookie('userName');
   if(userName) {
      var textHtml = '\
                     <li id="user-account" class="b-topLink__accountLinks__menuEntry"><a href="#" title="Đăng nhập" rel="follow">Tài khoản</a></li>\
                     <li id="user-logout" class="b-topLink__accountLinks__menuEntry"><a href="#" title="Đăng ký" rel="follow">Đăng xuất</a></li>\
                     <li class="b-topLink__accountLinks__menuEntry"><a href="#boxTrackOrder" class="js-trackOrder" data-toggle="modal" data-target="#boxTrackOrder">Kiểm tra đơn hàng</a></li>';
      ISS.DOM.html('#account_list', textHtml);
   }
   else {
      textHtml = '\
                     <li id="user-login" class="b-topLink__accountLinks__menuEntry"><a href="#" title="Đăng nhập" rel="follow">Đăng nhập</a></li>\
                     <li id="user-register" class="b-topLink__accountLinks__menuEntry"><a href="#" title="Đăng ký" rel="follow">Đăng ký</a></li>\
                     <li class="b-topLink__accountLinks__menuEntry"><a href="#boxTrackOrder" class="js-trackOrder" data-toggle="modal" data-target="#boxTrackOrder">Kiểm tra đơn hàng</a></li>';
      ISS.DOM.html('#account_list', textHtml);
   }
}

function bindEventLoadInfoUser() {
   var userName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName'): ISS.COOKIE.getCookie('userName');
   if(userName) {
      ISS.EVENT.bindEvent('#user-account', 'click', function () {
         ISS.WINDOW.location('/jsp/user/user-account.jsp');
      });

      ISS.EVENT.bindEvent('#user-logout', 'click', function () {
         userLogout();
      });
   }
   else {
      ISS.EVENT.bindEvent('#user-login', 'click', function () {
         ISS.WINDOW.location('/jsp/user/user-login.jsp');
      });

      ISS.EVENT.bindEvent('#user-register', 'click', function () {
         ISS.WINDOW.location('/jsp/user/user-register.jsp');
      });
   }
}

function userLogout() {
   ISS.SESSION.removeSession('userName');
   ISS.COOKIE.setCookie('userName', '', 0);
   ISS.WINDOW.currentLocation();
}

function renderMenu() {
   renderGender();
   getListMenu();
}

function renderCart(){
   getListCart();
}

function getListCart(){
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getListCart',
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getListCartComplete(responseInfo);
      }
   });
}

function getListCartComplete(listCart) {
   if(listCart) {
      _lstCart = listCart;
      changePriceToString(_lstCart);
      mapCartToView(_lstCart);
   }
}

function changePriceToString(listCart) {
   var _carts = listCart.carts;
   listCart.totalMoney = ISS.STRING_UTIL.formatNumeral(listCart.totalMoney, '0,0');
   if(_carts.length > 0) {
      for(var i = 0; i < _carts.length; i++) {
         _carts[i].giaBan = ISS.STRING_UTIL.formatNumeral(_carts[i].giaBan, '0,0');
      }
   }
}

function renderGender(){
   var gender = ISS.SESSION.getSession('session_gender');
   if(gender === 'NU') {
      ISS.DOM.addClass('#gender_0', 'active');
      ISS.DOM.removeClass('#gender_1', 'active');
   }

   if(gender === 'NAM') {
      ISS.DOM.addClass('#gender_1', 'active');
      ISS.DOM.removeClass('#gender_0', 'active');
   }
}

function getListMenu() {
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/women/getListMenu/' + ISS.SESSION.getSession('session_gender'),
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getListMenuComplete(responseInfo);
      }
   });
}


function getListMenuComplete(listDanhMuc){
   var template = '';
   for(var i = 0; i < listDanhMuc.length; i++){
      //get list danh muc san pham
      var _loaiDanhMucSanPham = listDanhMuc[i].loaiDanhMucSanPham;
      var detail = '';
      for(var j = 0; j < _loaiDanhMucSanPham.length; j++) {
         var _href = ISS.SESSION.getSession('contextPath') + '/service/product/toPageProduct/'+ ISS.SESSION.getSession('session_gender') + '/' + _loaiDanhMucSanPham[j].maLoaiDMSP;
         detail += ISS.STRING_UTIL.format(TEMPLATE.Menu.itemDetail,{
            href: _href,
            danhMucSP: _loaiDanhMucSanPham[j].tenLoaiDMSP,
            id: _loaiDanhMucSanPham[j].maLoaiDMSP
         });
      }
      //get danh muc san pham
      var item = ISS.STRING_UTIL.format(TEMPLATE.Menu.item,{
         tenDanhMuc: listDanhMuc[i].tenLoai,
         listDetail: detail
      });
      // add menu
      template += item;
   }
   // add tu van
   template += ISS.STRING_UTIL.format(TEMPLATE.Menu.consultant,{
      contextPath: ISS.SESSION.getSession('contextPath')
   });

   ISS.DOM.html('#sub-menu', template);
}

function mapCartToView(gioHang){
   if(gioHang.total > 0) {
      gioHang.contextPath = ISS.SESSION.getSession('contextPath');
      var _cartHTML = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.Cart.shopCart ,gioHang);
      delete gioHang.contextPath;
      ISS.DOM.html('#head_shop_cart', _cartHTML);
      ISS.DOM.html('#shopping-bag-qty', gioHang.total);
   }
   else {
      ISS.DOM.html('#head_shop_cart', '');
      ISS.DOM.html('#shopping-bag-qty', '0');
   }
}